#include "GDVFrame.h"
#include "MyCanvas.h"
#include "MyCanvasGL.h"
#include <wx/splitter.h>
#include <iostream>

enum {
  ID_EXAMPLES_MESSAGE = wxID_HIGHEST+1,
  ID_EXAMPLES_INPUT,
  ID_SLIDER_LEFT,
  ID_SLIDER_RIGHT,
};

BEGIN_EVENT_TABLE(GDVFrame, wxFrame)
  EVT_MENU(wxID_OPEN,  GDVFrame::onFileOpen)
  EVT_MENU(wxID_EXIT,  GDVFrame::onFileExit)
  EVT_MENU(ID_EXAMPLES_MESSAGE, GDVFrame::onExamplesMessage)
  EVT_MENU(ID_EXAMPLES_INPUT,   GDVFrame::onExamplesInput)
  EVT_SLIDER(ID_SLIDER_LEFT, GDVFrame::onSliderLeft)
  EVT_SLIDER(ID_SLIDER_RIGHT, GDVFrame::onSliderRight)
END_EVENT_TABLE()


// Default Konstruktor
// Erzeugt alle GUI Elemente und legt die Anordnung im Fenster fest.
GDVFrame::GDVFrame() : wxFrame(0, wxID_ANY, wxT("GDV-Vertiefungskurs"),
      wxDefaultPosition, wxSize(800,600)) {
  // Setze IO Dirs
  _idir = wxGetCwd();

  // TODO: Hier Menus hinzuf�gen:

  // File Menu
  wxMenu* fileMenu = new wxMenu;
  fileMenu->Append(wxID_OPEN, wxT("Open"));
  fileMenu->AppendSeparator();
  fileMenu->Append(wxID_EXIT, wxT("Exit"));

  // Examples Menu
  wxMenu* examplesMenu = new wxMenu;
  examplesMenu->Append(ID_EXAMPLES_MESSAGE,wxT("Message"));
  examplesMenu->Append(ID_EXAMPLES_INPUT,  wxT("Input"));

  // MenuBar
  wxMenuBar *menuBar = new wxMenuBar;
  menuBar->Append(fileMenu,     wxT("File"));
  menuBar->Append(examplesMenu, wxT("Examples"));
  SetMenuBar(menuBar);

  // Statusbar
  CreateStatusBar(2);
  SetStatusBarPane(-1); // disable automatic help

  wxSplitterWindow* splitter = new wxSplitterWindow(this, wxID_ANY);

  wxPanel *win_left = new wxPanel(splitter, wxID_ANY);
  wxBoxSizer *box_left = new wxBoxSizer(wxVERTICAL); 
  _canvas = new MyCanvas(win_left, wxID_ANY);
  wxSlider *slider_left = new wxSlider(win_left, ID_SLIDER_LEFT, 50, 0, 100);
  box_left->Add(_canvas,1,wxEXPAND);
  box_left->Add(slider_left,0,wxEXPAND);
  win_left->SetSizer(box_left);

  wxPanel *win_right = new wxPanel(splitter, wxID_ANY);
  wxBoxSizer *box_right = new wxBoxSizer(wxVERTICAL); 
  _canvasgl = new MyCanvasGL(win_right, wxID_ANY);
  wxSlider *slider_right = new wxSlider(win_right, ID_SLIDER_RIGHT, 50, 0, 100);
  box_right->Add(_canvasgl,1,wxEXPAND);
  box_right->Add(slider_right,0,wxEXPAND);
  win_right->SetSizer(box_right);

  // Fuege Canvas hinzu	
  splitter->SplitVertically(win_left, win_right);
}


// Callback f�r den "File -> Open" Men�punkt
void GDVFrame::onFileOpen(wxCommandEvent &event) {
  wxString filename = wxFileSelector(wxT("Choose a file to open"), _idir,
      wxT(""), wxT("*.dat"),
      wxT("Janus files (*.dat)|*.dat|All files (*.*)|*.*"),wxOPEN);

  if (!filename.empty()) {
    _idir = wxPathOnly(filename);
    SetStatusText(wxFileNameFromPath(filename),0);

    // TODO: hier mit der Datei arbeiten (�ffnen, einlesen, etc.):
    std::cout << "GDvFrame::OnFileOpen: " << filename << std::endl;
  }
  //else: cancelled by user
}


// Callback f�r den "File -> Exit" Men�punkt
void GDVFrame::onFileExit(wxCommandEvent& event) {
  Close();
}


// Callback f�r den "Example -> Message" Men�punkt
void GDVFrame::onExamplesMessage(wxCommandEvent &event) {
  wxString message;
  message << wxT("Here is a message for you!");
  wxMessageBox(message,wxT("Message Example"), wxOK);
}


// Callback f�r den "Example -> Input" Men�punkt
void GDVFrame::onExamplesInput(wxCommandEvent &event) {
  wxString inputstr = wxGetTextFromUser(wxT("Set Epsilon"),
      wxT("Input Example"),wxT("0.0001"));
  double eps;

  if (inputstr.ToDouble(&eps)) {
    std::cout << "GDVFrame::OnExamplesInput: double eps = " << eps << std::endl;
  }
}


// Callback f�r linken Slider
void GDVFrame::onSliderLeft(wxCommandEvent &event) {
  std::cout << "GDVFrame::onSliderLeft: value=" << event.GetInt()
      << std::endl;
}


// Callback f�r den rechten Slider
void GDVFrame::onSliderRight(wxCommandEvent &event) {
  std::cout << "GDVFrame::onSliderRightt: value=" << event.GetInt()
      << std::endl;
}



